<script>
class Observer{
    constructor(){
        this.message = {}
    }
    on = (type, fn) => {
        if(!this.message[type]){
            this.message[type] = []
        }
        this.message[type].push(fn)
    }
    off = (type, fn) => {
        if(!fn){
            delete this.message[type]
        }else{
            this.message = this.message[type].filter(item => item !== fn)
        }
    }
    //触发
    emit = (type) => {
        if(!this.message[type]) return
        this.message[type].forEach(item => {
            item()
        })
    }
}

let p1 = new Observer()
p1.on('type1', handlerA)
p1.on('type1', handlerB)
p1.on('type2', handlerC)
function handlerA(){console.log('handlerA')}
function handlerB(){console.log('handlerB')}
function handlerC(){console.log('handlerC')}

// p1.off('type1')
p1.emit('type1')

console.log(p1.message)




</script>